package com.se.nju.rtvps.peerManagement;

import java.util.ArrayList;

public class TreeNode {
	
	private int maxChildLength = 3;
	private Object object; //stands for "PeerID"
	private int currentChild = 0;
	
	ArrayList<TreeNode> children = new ArrayList<TreeNode>(maxChildLength);
	
	public TreeNode(Object aObject){
		this.object = aObject;
	}
	
	public boolean addChild(TreeNode o){
		if(currentChild < maxChildLength){
			children.add(o);
			currentChild++;
			return true;
		}else{
			for(int i=0; i < children.size(); i++){
				return children.get(i).addChild(o);
			}
		}
		return false;
	}
	
	//remove the left children from current node
	public boolean removeChild(TreeNode o){
		for(int index=0; index < children.size(); index++){
			TreeNode node = children.get(index);
			if(node.getValue().equals(o.getValue())){
				children.remove(node);
				currentChild--;
				return true;
			}
			else{
				return node.removeChild(o);
			}
		}
		return false;
	}
	
	public boolean isFull(){
		return (currentChild < maxChildLength) ? true : false;
	}

	public boolean hasAChild(){
		return (currentChild == 0) ? false : true;
	}
	
	public ArrayList<TreeNode> getChildren(){
		return children;
	}
	
	public Object getValue(){
		return object;
	}
}
